﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Grain.Crm.ExcelExtractor.Input;
using Grain.Crm.ExcelImporter;

namespace Grain.Crm.ExcelExtractor
{
    public class FileContentHelper
    {
        public static string GenerateContentRow(SheetDescription sheetDescription, EnumerableRowCollection<DataRow> collection, string entityName)
        {
            return GenerateContentRow(sheetDescription, collection, entityName, 0);
        }

        public static string GenerateContentRow(SheetDescription sheetDescription, EnumerableRowCollection<DataRow> collection, string entityName, int startAtRow)
        {
            var contentRowBuilder = new StringBuilder();
            int seq = 0;

            foreach (var description in sheetDescription.Descriptions)
            {
                if (description.IsSpecial || description.CrmEntity != entityName) continue;

                var value = XlsValueRetriever.GetValueByDescription(collection, description, startAtRow);

                if (seq > 0)
                    contentRowBuilder.Append(",");

                contentRowBuilder.Append(value);

                seq++;
            }

            return contentRowBuilder.ToString();
        }

        public static string GenerateHeaderRow(SheetDescription sheetDescription, string entityName)
        {
            var headerRowBuilder = new StringBuilder();

            foreach (var description in sheetDescription.Descriptions)
            {
                if (description.IsSpecial || description.CrmEntity != entityName) continue;

                if (headerRowBuilder.Length > 0)
                    headerRowBuilder.Append(",");
                headerRowBuilder.Append(description.CrmDisplayName);
            }

            return headerRowBuilder.ToString();
        }
    }
}
